home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdImageProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  7.3 KB  |  263 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Pete Collins
  22.  *   Brian King
  23.  *   Ben Goodger
  24.  */
  25.  
  26. var gAnchorElement = null;
  27. var gOriginalHref = "";
  28. var gHNodeArray = [];
  29.  
  30. // dialog initialization code
  31.  
  32. function Startup()
  33. {
  34.   if (!InitEditorShell())
  35.     return;
  36.  
  37.   ImageStartup();
  38.   gDialog.hrefInput        = document.getElementById("hrefInput");
  39.   gDialog.makeRelativeLink = document.getElementById("MakeRelativeLink");
  40.   gDialog.showLinkBorder   = document.getElementById("showLinkBorder");
  41.  
  42.   // Get a single selected image element
  43.   var tagName = "img";
  44.   if ("arguments" in window && window.arguments[0])
  45.   {
  46.     imageElement = window.arguments[0];
  47.     // We've been called from form field propertes, so we can't insert a link
  48.     var imageLinkTab = document.getElementById('imageLinkTab');
  49.     imageLinkTab.parentNode.removeChild(imageLinkTab);
  50.   }
  51.   else
  52.   {
  53.     // First check for <input type="image">
  54.     imageElement = editorShell.GetSelectedElement("input");
  55.     if (!imageElement || imageElement.getAttribute("type") != "image") {
  56.       // Get a single selected image element
  57.       imageElement = editorShell.GetSelectedElement(tagName);
  58.       if (imageElement)
  59.         gAnchorElement = editorShell.GetElementOrParentByTagName("href", imageElement);
  60.     }
  61.   }
  62.  
  63.   if (imageElement)
  64.   {
  65.     // We found an element and don't need to insert one
  66.     if (imageElement.hasAttribute("src"))
  67.     {
  68.       gInsertNewImage = false;
  69.       gActualWidth  = imageElement.naturalWidth;
  70.       gActualHeight = imageElement.naturalHeight;
  71.     }
  72.   }
  73.   else
  74.   {
  75.     gInsertNewImage = true;
  76.  
  77.     // We don't have an element selected,
  78.     //  so create one with default attributes
  79.  
  80.     imageElement = editorShell.CreateElementWithDefaults(tagName);
  81.     if (!imageElement)
  82.     {
  83.       dump("Failed to get selected element or create a new one!\n");
  84.       window.close();
  85.       return;
  86.     }
  87.     gAnchorElement = editorShell.GetSelectedElement(tagName);
  88.   }
  89.  
  90.   // Make a copy to use for AdvancedEdit
  91.   globalElement = imageElement.cloneNode(false);
  92.  
  93.   // We only need to test for this once per dialog load
  94.   gHaveDocumentUrl = GetDocumentBaseUrl();
  95.  
  96.   InitDialog();
  97.   if (gAnchorElement)
  98.     gOriginalHref = gAnchorElement.getAttribute("href");
  99.   gDialog.hrefInput.value = gOriginalHref;
  100.  
  101.   FillLinkMenulist(gDialog.hrefInput, gHNodeArray);
  102.   ChangeLinkLocation();
  103.  
  104.   // Save initial source URL
  105.   gOriginalSrc = gDialog.srcInput.value;
  106.  
  107.   // By default turn constrain on, but both width and height must be in pixels
  108.   gDialog.constrainCheckbox.checked =
  109.     gDialog.widthUnitsMenulist.selectedIndex == 0 &&
  110.     gDialog.heightUnitsMenulist.selectedIndex == 0;
  111.  
  112.   SetTextboxFocus(gDialog.srcInput);
  113.  
  114.   SetWindowLocation();
  115. }
  116.  
  117. // Set dialog widgets with attribute data
  118. // We get them from globalElement copy so this can be used
  119. //   by AdvancedEdit(), which is shared by all property dialogs
  120. function InitDialog()
  121. {
  122.   InitImage();
  123.   gDialog.showLinkBorder.checked = gDialog.border.value != "0";
  124. }
  125.  
  126. function ChangeLinkLocation()
  127. {
  128.   SetRelativeCheckbox(gDialog.makeRelativeLink);
  129.   gDialog.showLinkBorder.disabled = !TrimString(gDialog.hrefInput.value);
  130. }
  131.  
  132. function ToggleShowLinkBorder()
  133. {
  134.   if (gDialog.showLinkBorder.checked)
  135.   {
  136.     if (TrimString(gDialog.border.value) == "0")
  137.       gDialog.border.value = "";
  138.   }
  139.   else
  140.   {
  141.     gDialog.border.value = "0";
  142.   }
  143. }
  144.  
  145. // Get data from widgets, validate, and set for the global element
  146. //   accessible to AdvancedEdit() [in EdDialogCommon.js]
  147. function ValidateData()
  148. {
  149.   return ValidateImage();
  150. }
  151.  
  152. function doHelpButton()
  153. {
  154.   openHelp("image_properties");
  155.   return true;
  156. }
  157.  
  158. function onAccept()
  159. {
  160.   // Use this now (default = false) so Advanced Edit button dialog doesn't trigger error message
  161.   gDoAltTextError = true;
  162.  
  163.   if (ValidateData())
  164.   {
  165.     if ("arguments" in window && window.arguments[0])
  166.     {
  167.       SaveWindowLocation();
  168.       return true;
  169.     }
  170.  
  171.     editorShell.BeginBatchChanges();
  172.  
  173.     try
  174.     {
  175.       if (gRemoveImageMap)
  176.       {
  177.         globalElement.removeAttribute("usemap");
  178.         if (gImageMap)
  179.         {
  180.           editorShell.DeleteElement(gImageMap);
  181.           gInsertNewIMap = true;
  182.           gImageMap = null;
  183.         }
  184.       }
  185.       else if (gImageMap)
  186.       {
  187.         // Assign to map if there is one
  188.         var mapName = gImageMap.getAttribute("name");
  189.         if (mapName != "")
  190.         {
  191.           globalElement.setAttribute("usemap", ("#"+mapName));
  192.           if (globalElement.getAttribute("border") == "")
  193.             globalElement.setAttribute("border", 0);
  194.         }
  195.  
  196.         if (gInsertNewIMap)
  197.         {
  198.           editorShell.editorDocument.body.appendChild(gImageMap);
  199.         //editorShell.InsertElementAtSelection(gImageMap, false);
  200.         }
  201.       }
  202.  
  203.       // Create or remove the link as appropriate
  204.       var href = gDialog.hrefInput.value;
  205.       if (href != gOriginalHref) {
  206.         if (href)
  207.           editorShell.SetTextProperty("a", "href", href);
  208.         else
  209.           editorShell.RemoveTextProperty("href", "");
  210.       }
  211.  
  212.       // All values are valid - copy to actual element in doc or
  213.       //   element created to insert
  214.       editorShell.CloneAttributes(imageElement, globalElement);
  215.       if (gInsertNewImage)
  216.       {
  217.         // 'true' means delete the selection before inserting
  218.         editorShell.InsertElementAtSelection(imageElement, true);
  219.         // Also move the insertion point out of the link
  220.         if (href)
  221.           setTimeout(editorShell.RemoveTextProperty, 0, "href", "");
  222.       }
  223.  
  224.       // Check to see if the link was to a heading
  225.       // Do this last because it moves the caret (BAD!)
  226.       var index = gDialog.hrefInput.selectedIndex;
  227.       if (index in gHNodeArray && gHNodeArray[index])
  228.       {
  229.         var anchorNode = editorShell.editorDocument.createElement("a");
  230.         if (anchorNode)
  231.         {
  232.           anchorNode.name = href.substr(1);
  233.           // Remember to use editorShell method so it is undoable!
  234.           editorShell.InsertElement(anchorNode, gHNodeArray[index], 0, false);
  235.         }
  236.       }
  237.  
  238.       // un-comment to see that inserting image maps does not work!
  239.       /*test = editorShell.CreateElementWithDefaults("map");
  240.       test.setAttribute("name", "testing");
  241.       testArea = editorShell.CreateElementWithDefaults("area");
  242.       testArea.setAttribute("shape", "circle");
  243.       testArea.setAttribute("coords", "86,102,52");
  244.       testArea.setAttribute("href", "test");
  245.       test.appendChild(testArea);
  246.       editorShell.InsertElementAtSelection(test, false);*/
  247.     }
  248.     catch (e)
  249.     {
  250.       dump(e);
  251.     }
  252.  
  253.     editorShell.EndBatchChanges();
  254.  
  255.     SaveWindowLocation();
  256.     return true;
  257.   }
  258.  
  259.   gDoAltTextError = false;
  260.  
  261.   return false;
  262. }
  263.